package com.iaiai.browser.core.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * 
 * <p>
 * Title: IndexController.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-11-2
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class FileUtil {

	/**
	 * 读取文件
	 * 
	 * @param filename
	 * @return
	 */
	public static byte[] readFile(String filename) {
		try {
			File file = new File(filename);
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));// 字节方式读取
			byte[] bytes = new byte[(int) file.length()];// 设置缓冲区大小
			byte[] n = new byte[1];
			int i = 0;
			for (; in.read(n) >= 0; i++) {
				bytes[i] = n[0];
			}
			in.close();
			return bytes;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 返回图片
	 * 
	 * @param b
	 * @return
	 */
	public static Bitmap Bytes2Bimap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}

}
